//
//  solution.swift
//  206-翻转链表
//
//  Created by lhc on 2023/9/20.
//

import Foundation

class Solution<T> {
    
    static func reverseList(head: ListNode<T>?) -> ListNode<T>? {
        guard let head = head else {return nil}
        
        var pre: ListNode<T>? = nil
        var cur: ListNode<T>? = head
        
        while cur != nil {
            let next = cur?.next
            cur?.next = pre
            pre = cur
            cur = next
        }
        
        return pre
    }
    
    static func reverseListByCursion(head: ListNode<T>?) -> ListNode<T>? {
        guard let head = head else {return nil}
        guard head.next != nil else {return nil}
        
        let rev: ListNode<T>? = reverseList(head: head.next)
        head.next?.next = head
        head.next = nil
        
        return rev
    }
}
